import request from '../utils/request'

// 通用接口响应类型
export interface ApiResponse<T> {
  code: number
  message: string
  data: T
}

// 秒杀商品接口类型
export interface KillItem {
  id: number
  merchantId: number
  name: string
  price: number
  stock: number
  image: string
  category: string
  isUserAddCategory: number
  brand: string
  spec: string
  sold: number
  score: number
  status: number
  isKill: number
  killPrice: number
  createTime: string
  updateTime: string
}

// 获取秒杀商品列表
export const getKillItemsService = (): Promise<ApiResponse<KillItem[]>> => {
  // 由于在 axios 响应拦截器中已经返回了 response.data，这里通过类型断言
  // 将 Axios 的返回值视为后端的统一响应结构 ApiResponse<KillItem[]>
  return request.get('/kill/find') as any
}

// 秒杀订单响应类型
export interface KillOrderResponse {
  orderId: string
  amount: number
  createTime: string
}

// 秒杀商品直接下单
export const addKillOrderService = (
  itemId: number,
  num: number,
  addressId: number,
  spec: string
): Promise<ApiResponse<KillOrderResponse>> => {
  // 这里同样通过类型断言，将返回结果视为 ApiResponse<KillOrderResponse>
  return request.post<KillOrderResponse>('/itemOrder/addItemOrder', null, {
    params: {
      itemId,
      num,
      addressId,
      spec
    }
  }) as any
}
